<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Adm_Register extends Controller_Base_Backend {

	public function action_index() {
		$this->view->body->content = Views::Register(isset($_GET["redirect"]) ? $_GET["redirect"] : '','auth/adm/register.tpl');
		$this->response->body($this->view);
	}

	public function action_register() {
		$user = Model_User::factory();
		try {
			if(Model_User::confirm_password($_POST['password'], $_POST['password_confirm'])) {
				if(Model_User::unique_email($_POST['email'])) {
					$user->set_name($_POST['name']);
					$user->set_email($_POST['email']);
					$user->set_password(Model_User::hash($_POST['password']));
					$user->save();
					$user->add_role('login');
					Request::current()->redirect("/adm/user/list");
				}
			}
		} catch (Validation_Exception $e) {
			$this->response->body(Json_View::factory(array("content" => $e->errors())));
		} catch (Exception $e) {
			$errors = array("email" => $e->getMessage());
			$this->response->body(Json_View::factory(array("content" => $errors)));
		}
	}	
	
} // End Register